/*
 * Copyright (C) 2024 HiHope Open Source Organization.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import {startMiracastServiceCallback} from "./i_miracast_player_service";
import {stopMiracastServiceCallback} from "./i_miracast_player_service";
import {registerDeviceConnectListenerCallback} from "./i_miracast_player_service";
import {unRegisterDeviceConnectListenerCallback} from "./i_miracast_player_service";
import {setSurfaceCallback} from "./i_miracast_player_service";
import {playCallback} from "./i_miracast_player_service";
import {pauseCallback} from "./i_miracast_player_service";
import {stopCallback} from "./i_miracast_player_service";
import IMiracastPlayerService from "./i_miracast_player_service";
import rpc from "@ohos.rpc";
import IDeviceConnectListener from "./i_device_connect_listener";
import DeviceConnectListenerStub from "./device_connect_listener_stub";

export default class MiracastPlayerServiceStub extends rpc.RemoteObject implements IMiracastPlayerService {
    constructor(des: string) {
        super(des);
    }
    
    async onRemoteMessageRequest(code: number, data:rpc.MessageSequence, reply:rpc.MessageSequence, option:rpc.MessageOption): Promise<boolean> {
        console.log("onRemoteMessageRequest called, code = " + code);
        switch(code) {
            case MiracastPlayerServiceStub.COMMAND_START_MIRACAST_SERVICE: {
                let promise = new Promise<void>((resolve,reject) => { 
                    this.startMiracastService((errCode, returnValue) => {
                        reply.writeInt(errCode);
                        if (errCode == 0) {
                            reply.writeInt(returnValue);
                        }
                        resolve();
                    });
                });
                await promise;
                return true;
            }
            case MiracastPlayerServiceStub.COMMAND_STOP_MIRACAST_SERVICE: {
                let promise = new Promise<void>((resolve,reject) => { 
                    this.stopMiracastService((errCode, returnValue) => {
                        reply.writeInt(errCode);
                        if (errCode == 0) {
                            reply.writeInt(returnValue);
                        }
                        resolve();
                    });
                });
                await promise;
                return true;
            }
            case MiracastPlayerServiceStub.COMMAND_REGISTER_DEVICE_CONNECT_LISTENER: {
                let listenerVar = data.readRemoteObject();
                let promise = new Promise<void>((resolve,reject) => {
                    this.registerDeviceConnectListener(listenerVar as unknown as IDeviceConnectListener, (errCode, returnValue) => {
                        reply.writeInt(errCode);
                        if (errCode == 0) {
                            reply.writeInt(returnValue);
                        }
                        resolve();
                    });
                });
                await promise;
                return true;
            }
            case MiracastPlayerServiceStub.COMMAND_UN_REGISTER_DEVICE_CONNECT_LISTENER: {
                let listenerVar = data.readRemoteObject();
                let promise = new Promise<void>((resolve,reject) => { 
                    this.unRegisterDeviceConnectListener(listenerVar as unknown as IDeviceConnectListener, (errCode, returnValue) => {
                        reply.writeInt(errCode);
                        if (errCode == 0) {
                            reply.writeInt(returnValue);
                        }
                        resolve();
                    });
                });
                await promise;
                return true;
            }
            case MiracastPlayerServiceStub.COMMAND_SET_SURFACE: {
                let surfaceIdVar = data.readString();
                let promise = new Promise<void>((resolve,reject) => { 
                    this.setSurface(surfaceIdVar, (errCode, returnValue) => {
                        reply.writeInt(errCode);
                        if (errCode == 0) {
                            reply.writeInt(returnValue);
                        }
                        resolve();
                    });
                });
                await promise;
                return true;
            }
            case MiracastPlayerServiceStub.COMMAND_PLAY: {
                let deviceIdVar = data.readString();
                let promise = new Promise<void>((resolve,reject) => { 
                    this.play(deviceIdVar, (errCode, returnValue) => {
                        reply.writeInt(errCode);
                        if (errCode == 0) {
                            reply.writeInt(returnValue);
                        }
                        resolve();
                    });
                });
                await promise;
                return true;
            }
            case MiracastPlayerServiceStub.COMMAND_PAUSE: {
                let deviceIdVar = data.readString();
                let promise = new Promise<void>((resolve,reject) => { 
                    this.pause(deviceIdVar, (errCode, returnValue) => {
                        reply.writeInt(errCode);
                        if (errCode == 0) {
                            reply.writeInt(returnValue);
                        }
                        resolve();
                    });
                });
                await promise;
                return true;
            }
            case MiracastPlayerServiceStub.COMMAND_STOP: {
                let deviceIdVar = data.readString();
                let promise = new Promise<void>((resolve,reject) => { 
                    this.stop(deviceIdVar, (errCode, returnValue) => {
                        reply.writeInt(errCode);
                        if (errCode == 0) {
                            reply.writeInt(returnValue);
                        }
                        resolve();
                    });
                });
                await promise;
                return true;
            }
            default: {
                console.log("invalid request code" + code);
                break;
            }
        }
        return false;
    }
    
    startMiracastService(callback: startMiracastServiceCallback): void{}
    stopMiracastService(callback: stopMiracastServiceCallback): void{}
    registerDeviceConnectListener(listener: IDeviceConnectListener, callback: registerDeviceConnectListenerCallback): void{}
    unRegisterDeviceConnectListener(listener: IDeviceConnectListener, callback: unRegisterDeviceConnectListenerCallback): void{}
    setSurface(surfaceId: string, callback: setSurfaceCallback): void{}
    play(deviceId: string, callback: playCallback): void{}
    pause(deviceId: string, callback: pauseCallback): void{}
    stop(deviceId: string, callback: stopCallback): void{}

    static readonly COMMAND_START_MIRACAST_SERVICE = 1;
    static readonly COMMAND_STOP_MIRACAST_SERVICE = 2;
    static readonly COMMAND_REGISTER_DEVICE_CONNECT_LISTENER = 3;
    static readonly COMMAND_UN_REGISTER_DEVICE_CONNECT_LISTENER = 4;
    static readonly COMMAND_SET_SURFACE = 5;
    static readonly COMMAND_PLAY = 6;
    static readonly COMMAND_PAUSE = 7;
    static readonly COMMAND_STOP = 8;
}

